package com.example.scale;

import org.andengine.engine.camera.Camera;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.scene.menu.item.decorator.ScaleMenuItemDecorator;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.util.GLState;
import org.andengine.util.adt.align.HorizontalAlign;
import org.andengine.util.adt.color.Color;

import com.example.scale.SceneManager.SceneType;

public class OptionsScene extends BaseScene implements IOnMenuItemClickListener {

	private MenuScene menuChildScene;
	
	private MenuScene mPopupScene;
	private boolean popupIsShowing;
	
	private IMenuItem toggleStoryCheckBox;
	private boolean storyBoxChecked;
	private IMenuItem changeControlMethod;
	private int controlMethodPref;
	private IMenuItem saveSettingsButton;
	private IMenuItem toggleHintCheckBox;
	private boolean toggleHintBoxChecked;
	
	private final int TOGGLE_STORY = 0;
	private final int SAVE_SETTINGS = 1;
	private final int CONTROL_METHOD = 2;
	private final int TOGGLE_HINTS = 3;
	private final int MENU_YES = 4;
	private final int MENU_NO = 5;
	private final int MENU_CANCEL = 6;
	
	private SavedData mySavedData;
	
	@Override
	public void createScene() {
		// Get saved data from shared preferences class
		createPopupScene();
		mySavedData = new SavedData(activity);
		loadSavedData();
		
		// Add Background
		attachChild(new Sprite(GameActivity.CAMERA_WIDTH/2, GameActivity.CAMERA_HEIGHT/2,
				resourcesManager.blank_background_region, vbom) {
			@Override
			protected void preDraw(GLState pGLState, Camera pCamera) {
				super.preDraw(pGLState, pCamera);
				pGLState.enableDither();
			}
		});
		
		menuChildScene = new MenuScene(camera);
		menuChildScene.setPosition(0, 0);

		Sprite optionsHeader = new Sprite(GameActivity.CAMERA_WIDTH/2, GameActivity.CAMERA_HEIGHT-60,
				resourcesManager.options_region, vbom);
		optionsHeader.setScale(.6f);
		attachChild(optionsHeader);
		
		/*Sprite saveSettingsSprite = new Sprite(GameActivity.CAMERA_WIDTH/2, 60,
				resourcesManager.save_settings_region, vbom) {
			public boolean onAreaTouched(TouchEvent touchEvent, float X, float Y) {
				if (touchEvent.isActionDown()) {
					
				}
				return true;
			};
		};
		attachChild(saveSettingsSprite);
		registerTouchArea(saveSettingsSprite);*/
		
		final Sprite backButton = new Sprite(45, GameActivity.CAMERA_HEIGHT-45, resourcesManager.arrow_region, vbom) {
			public boolean onAreaTouched(TouchEvent touchEvent, float X, float Y) {
				if (touchEvent.isActionDown()) {
					onBackKeyPressed();
				}
				return true;
			}
		};
		backButton.setScale(0.5f);
		attachChild(backButton);
		registerTouchArea(backButton);
		
		/*Text OptionsText = new Text(GameActivity.CAMERA_WIDTH / 2,
				GameActivity.CAMERA_HEIGHT - 30, resourcesManager.font,
				"Options", vbom);
		OptionsText.setColor(Color.WHITE);
		attachChild(OptionsText);*/
		
		Text ControlMethodText = new Text(GameActivity.CAMERA_WIDTH / 4+28,
				6*GameActivity.CAMERA_HEIGHT / 12, resourcesManager.font,
				"Control Method", vbom);
		//ControlMethodText.setScale(1.5f);
		ControlMethodText.setColor(Color.WHITE);
		attachChild(ControlMethodText);
		
		resourcesManager.control_method_tiled_region.setCurrentTileIndex(controlMethodPref);
		changeControlMethod = new ScaleMenuItemDecorator(
				new SpriteMenuItem(CONTROL_METHOD, resourcesManager.control_method_tiled_region,
						vbom), .9f, .7f);
		menuChildScene.addMenuItem(changeControlMethod);

		Text TogglePlotText = new Text(GameActivity.CAMERA_WIDTH / 4,
				8*GameActivity.CAMERA_HEIGHT / 12, resourcesManager.font,
				"Toggle Story", vbom);
		//TogglePlotText.setScale(1f);
		TogglePlotText.setColor(Color.WHITE);
		attachChild(TogglePlotText);
		
		Text ToggleHintText = new Text(GameActivity.CAMERA_WIDTH / 4,
				4*GameActivity.CAMERA_HEIGHT / 12, resourcesManager.font,
				"Toggle Hints", vbom);
		ToggleHintText.setColor(Color.WHITE);
		attachChild(ToggleHintText);
		
		/*Sprite toggleStoryWords = new Sprite(GameActivity.CAMERA_WIDTH / 4,
				2*GameActivity.CAMERA_HEIGHT / 3, resourcesManager.options_toggle_plot_region, vbom);
		attachChild(toggleStoryWords);*/
		
		if(storyBoxChecked) {
			resourcesManager.options_checkbox_tiled_region.setCurrentTileIndex(0);
		} else {
			resourcesManager.options_checkbox_tiled_region.setCurrentTileIndex(1);
		}
		toggleStoryCheckBox = new ScaleMenuItemDecorator(
				new SpriteMenuItem(TOGGLE_STORY, resourcesManager.options_checkbox_tiled_region,
						vbom), 1, .8f);
		menuChildScene.addMenuItem(toggleStoryCheckBox);
		
		if(toggleHintBoxChecked) {
			resourcesManager.options_checkbox_tiled_region.setCurrentTileIndex(0);
		} else {
			resourcesManager.options_checkbox_tiled_region.setCurrentTileIndex(1);
		}
		toggleHintCheckBox = new ScaleMenuItemDecorator(
				new SpriteMenuItem(TOGGLE_HINTS, resourcesManager.options_checkbox_tiled_region,
						vbom), 1, .8f);
		menuChildScene.addMenuItem(toggleHintCheckBox);
		
		saveSettingsButton = new ScaleMenuItemDecorator(new SpriteMenuItem(SAVE_SETTINGS, resourcesManager.save_settings_region, vbom),
				1, .8f);
		menuChildScene.addMenuItem(saveSettingsButton);
		
		menuChildScene.buildAnimations();
		menuChildScene.setBackgroundEnabled(false);
		
		toggleStoryCheckBox.setPosition(3*GameActivity.CAMERA_WIDTH / 4,
				2*GameActivity.CAMERA_HEIGHT / 3);
		saveSettingsButton.setPosition(GameActivity.CAMERA_WIDTH/2, 60);
		changeControlMethod.setPosition(3*GameActivity.CAMERA_WIDTH / 4,
				6*GameActivity.CAMERA_HEIGHT / 12+10);
		toggleHintCheckBox.setPosition(3*GameActivity.CAMERA_WIDTH / 4,
				4*GameActivity.CAMERA_HEIGHT / 12);

		menuChildScene.setOnMenuItemClickListener(this);
		setChildScene(menuChildScene);
	}
	
	public void loadSavedData() {
		mySavedData.loadPrefData();
		storyBoxChecked = mySavedData.togglePlotValue;
		controlMethodPref = mySavedData.controlMethodValue;
		toggleHintBoxChecked = mySavedData.toggleHintsValue;
	}
	
	public void showPopupScene() {
		setChildScene(mPopupScene, false, true, true);
		popupIsShowing = true;
	}
	
	public void createPopupScene() {
		popupIsShowing = false;
		mPopupScene = new MenuScene(camera);
		Rectangle textRectangle = new Rectangle(GameActivity.CAMERA_WIDTH / 2,
				GameActivity.CAMERA_HEIGHT / 2, GameActivity.CAMERA_WIDTH - 150,
				GameActivity.CAMERA_HEIGHT / 2+150, vbom);
		textRectangle.setColor(Color.BLACK);
		//textRectangle.setAlpha(.8f);
		mPopupScene.attachChild(textRectangle);
		
		//setIgnoreUpdate(true);
		
		final Text saveQuestionText = new Text(GameActivity.CAMERA_WIDTH / 2,
				GameActivity.CAMERA_HEIGHT / 2+100, resourcesManager.font,
				"Do you want to save your settings?",
				new TextOptions(HorizontalAlign.CENTER), vbom);
		mPopupScene.attachChild(saveQuestionText);
		
		final IMenuItem yesButton = new ScaleMenuItemDecorator(
				new SpriteMenuItem(MENU_YES, resourcesManager.yes_region,
						vbom), 0.6f, 0.5f);
		mPopupScene.addMenuItem(yesButton);
		final IMenuItem noButton = new ScaleMenuItemDecorator(
				new SpriteMenuItem(MENU_NO, resourcesManager.no_region,
						vbom), 0.6f, 0.5f);
		mPopupScene.addMenuItem(noButton);
		final IMenuItem cancelButton = new ScaleMenuItemDecorator(
				new SpriteMenuItem(MENU_CANCEL, resourcesManager.cancel_region,
						vbom), 0.6f, 0.5f);
		mPopupScene.addMenuItem(cancelButton);
		
		mPopupScene.buildAnimations();
		mPopupScene.setBackgroundEnabled(false);
		
		yesButton.setPosition(GameActivity.CAMERA_WIDTH/2-125, 225);
		noButton.setPosition(GameActivity.CAMERA_WIDTH/2+125, 225);
		cancelButton.setPosition(GameActivity.CAMERA_WIDTH/2, 125);
		
		mPopupScene.setOnMenuItemClickListener(this);
	}
	
	public void goToMainMenu() {
		SceneManager.getInstance().optionsScene.disposeScene();
		ResourcesManager.getInstance().unloadOptionsTextures();
		SceneManager.getInstance().loadMenuScene(engine);
	}

	@Override
	public void onBackKeyPressed() {
		if (storyBoxChecked==mySavedData.togglePlotValue && mySavedData.toggleHintsValue == toggleHintBoxChecked && mySavedData.controlMethodValue == controlMethodPref) {
			goToMainMenu();
		} else {
			showPopupScene();
		}
	}

	@Override
	public SceneType getSceneType() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void disposeScene() {
		// TODO Auto-generated method stub
		
	}
	
	public void saveData() {
		mySavedData.saveData("TOGGLE_PLOT", storyBoxChecked);
		mySavedData.saveData("CONTROL_METHOD", controlMethodPref);
		mySavedData.saveData("TOGGLE_HINTS", toggleHintBoxChecked);
	}

	@Override
	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
			float pMenuItemLocalX, float pMenuItemLocalY) {
		switch (pMenuItem.getID()) {
		case TOGGLE_STORY:
			menuChildScene.detachChild(toggleStoryCheckBox);
			if(!storyBoxChecked) {
			    resourcesManager.options_checkbox_tiled_region.setCurrentTileIndex(0);
			    storyBoxChecked=true;
			} else {
				resourcesManager.options_checkbox_tiled_region.setCurrentTileIndex(1);
				storyBoxChecked=false;
			}
			toggleStoryCheckBox = new ScaleMenuItemDecorator(
					new SpriteMenuItem(TOGGLE_STORY, resourcesManager.options_checkbox_tiled_region,
							vbom), 1, .8f);
		    menuChildScene.addMenuItem(toggleStoryCheckBox);
		    menuChildScene.buildAnimations();
			//menuChildScene.setBackgroundEnabled(false);
			toggleStoryCheckBox.setPosition(3*GameActivity.CAMERA_WIDTH / 4,
					2*GameActivity.CAMERA_HEIGHT / 3);
			saveSettingsButton.setPosition(GameActivity.CAMERA_WIDTH/2, 60);
			changeControlMethod.setPosition(3*GameActivity.CAMERA_WIDTH / 4,
					6*GameActivity.CAMERA_HEIGHT / 12+10);
			toggleHintCheckBox.setPosition(3*GameActivity.CAMERA_WIDTH / 4,
					4*GameActivity.CAMERA_HEIGHT / 12);
			//menuChildScene.setOnMenuItemClickListener(this);
			//setChildScene(menuChildScene);
			return true;
		case SAVE_SETTINGS:
			//if(storyBoxChecked!=mySavedData.togglePlotValue) {
			saveData();
			//}
			loadSavedData();
			return true;
		case CONTROL_METHOD:
			menuChildScene.detachChild(changeControlMethod);
			if(controlMethodPref==0) {
			    resourcesManager.control_method_tiled_region.setCurrentTileIndex(1);
			    controlMethodPref=1;
			} else if(controlMethodPref==1){
				resourcesManager.control_method_tiled_region.setCurrentTileIndex(2);
			    controlMethodPref=2;
			} else {
				resourcesManager.control_method_tiled_region.setCurrentTileIndex(0);
			    controlMethodPref=0;
			}
			changeControlMethod = new ScaleMenuItemDecorator(
					new SpriteMenuItem(CONTROL_METHOD, resourcesManager.control_method_tiled_region,
							vbom), .9f, .7f);
		    menuChildScene.addMenuItem(changeControlMethod);
		    menuChildScene.buildAnimations();
			toggleStoryCheckBox.setPosition(3*GameActivity.CAMERA_WIDTH / 4,
					2*GameActivity.CAMERA_HEIGHT / 3);
			saveSettingsButton.setPosition(GameActivity.CAMERA_WIDTH/2, 60);
			changeControlMethod.setPosition(3*GameActivity.CAMERA_WIDTH / 4,
					6*GameActivity.CAMERA_HEIGHT / 12+10);
			toggleHintCheckBox.setPosition(3*GameActivity.CAMERA_WIDTH / 4,
					4*GameActivity.CAMERA_HEIGHT / 12);
			return true;
		case TOGGLE_HINTS:
			menuChildScene.detachChild(toggleHintCheckBox);
			if(!toggleHintBoxChecked) {
			    resourcesManager.options_checkbox_tiled_region.setCurrentTileIndex(0);
			    toggleHintBoxChecked=true;
			} else {
				resourcesManager.options_checkbox_tiled_region.setCurrentTileIndex(1);
				toggleHintBoxChecked=false;
			}
			toggleHintCheckBox = new ScaleMenuItemDecorator(
					new SpriteMenuItem(TOGGLE_HINTS, resourcesManager.options_checkbox_tiled_region,
							vbom), 1, .8f);
		    menuChildScene.addMenuItem(toggleHintCheckBox);
		    menuChildScene.buildAnimations();
			//menuChildScene.setBackgroundEnabled(false);
			toggleStoryCheckBox.setPosition(3*GameActivity.CAMERA_WIDTH / 4,
					2*GameActivity.CAMERA_HEIGHT / 3);
			saveSettingsButton.setPosition(GameActivity.CAMERA_WIDTH/2, 60);
			changeControlMethod.setPosition(3*GameActivity.CAMERA_WIDTH / 4,
					6*GameActivity.CAMERA_HEIGHT / 12+10);
			toggleHintCheckBox.setPosition(3*GameActivity.CAMERA_WIDTH / 4,
					4*GameActivity.CAMERA_HEIGHT / 12);
			//menuChildScene.setOnMenuItemClickListener(this);
			//setChildScene(menuChildScene);
			return true;
		case MENU_YES:
			if (popupIsShowing) {
				saveData();
				goToMainMenu();
			}
			return true;
		case MENU_NO:
			goToMainMenu();
			return true;
		case MENU_CANCEL:
			if (popupIsShowing) {
				popupIsShowing = false;
				clearChildScene();
				setChildScene(menuChildScene);
			}
			return true;
		default:
			return false;
		}
	}

}
